package ManageGameHServer.code;

import org.json.JSONArray;
import org.json.JSONException;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import ManageGameHServer.conf.Setup;

public class LogicGameServerWorker extends Thread {
    public static Jedis createNewCon(){
        Jedis redisLink = new Jedis(Setup.GameToLog_RD_Url, Setup.GameToLog_RD_Port);
        redisLink.auth(Setup.GameToLog_RD_Auth);
        redisLink.select(Setup.GameToLog_RD_Table);
        return redisLink;
    }
    public void run(){
        //监听全球开服通道
        Jedis connectRedis = createNewCon();
        System.out.println("本服务器私有通道:"+"Platform_"+Setup.GameToLog_RD_ChannelName);
        connectRedis.subscribe(new LogicGameToPlatformServerWorker(),Setup.GameToLog_RD_ChannelName);
    }
}
class  LogicGameToPlatformServerWorker extends JedisPubSub {
    public static int memberInfoCount=0;//会员信息日志条数
    public static int memberEnterGameCount=0;//会员进入游戏日志条数
    public static int memberFeeCount=0;//会员手续费日志条数
    public static int memberFundFlowCount=0;//会员资金流水日志条数
    public static int memberOfflineCount=0;//会员下线日志条数
    public static int memberOnlineCount=0;//会员上线日志条数
    public static int serverCount=0;//服务器日志条数
    public static int memberQuitGameCount=0;//会员退出游戏日志条数
    public static int number=50000;//达到日志条数进行分库(单表数据量达到5万进行分库)
    @Override
    public void onMessage(String channel, String message) {       //收到消息会调用
        try {
            /*if(memberInfoCount>number||memberEnterGameCount>number||memberFeeCount>number||memberFundFlowCount>number||
                    memberOfflineCount>number||memberOnlineCount>number||serverCount>number||memberQuitGameCount>number){
                DBSwitch.DBSwitch();
            }*/
            System.out.println("收到消息："+message);
            JSONArray msg =new JSONArray(message);
            //验证监听的消息是否异常
            if(msg.length()>1){
                new ExplainMessage().explainMsgForSwitch(msg);
            }else {
                System.out.println("收到错误消息:消息内容："+msg);
            }
        }catch (JSONException j){
            j.printStackTrace();
            System.out.println("-------"+j.getMessage());
            System.out.println("-------"+j.getStackTrace());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-------"+e.getMessage());
            System.out.println("-------"+e.getStackTrace());
        }
    }
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {    //订阅了频道会调用
        System.out.println("redis " +channel+" 连接状态 "+subscribedChannels);
    }
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {   //取消订阅 会调用
        System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d", channel, subscribedChannels));
    }
}
